package com.zhbti.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    ListView listview;
    String [] names={"小红","小白","小黑","小蓝"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=findViewById(R.id.lv_list);
        MyBaseAdapter baseAdapter=new MyBaseAdapter();
        listview.setAdapter(baseAdapter);

    }
    class MyBaseAdapter extends BaseAdapter{
        public int getCount(){
            return names.length;
        }

        @Override
        public Object getItem(int i) {
            return names[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view= LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item,null);
            TextView textView=view.findViewById(R.id.textView2);
            textView.setText(names[i]);
            return view;
        }

    }
}